package com.leon.alishop.core.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.leon.alishop.core.enumeration.GoodsStatusEnum;
import com.leon.alishop.core.service.GoodsService;
import com.leon.alishop.core.util.AliShopStringUtils;
import com.leon.alishop.dao.GoodsInfoDao;
import com.leon.alishop.model.GoodsInfo;

@Service("goodsService")
public class GoodsServiceImpl implements GoodsService {

	private static Logger log = LoggerFactory.getLogger(GoodsServiceImpl.class);
	
	@Override
	public int insertGoodsInfoForCrawl(GoodsInfo goods) {
		if(goods == null)
			return 0;
		if(AliShopStringUtils.getCharLength(goods.getProviderGoodsImgInfo()) > 15000){
			log.error("GOODSID:"+goods.getId()+"\t商品图片信息太长！");
			return 0;
		}
		GoodsInfo gt = goodsInfoDao.selectByProviderTaobaoGoodsId(goods.getProviderTaobaoGoodsId());
		GoodsInfo gtTemp = new GoodsInfo();
		if(gt == null){
			goods.setStatus(GoodsStatusEnum.UNFOUND.ordinal());
			goodsInfoDao.insertSelective(goods);
		}else{
			gtTemp.setId(gt.getId());
			if(!gt.getProviderStatus().equals(goods.getProviderStatus())){
				gtTemp.setProviderStatus(goods.getProviderStatus());
			}
			gtTemp.setFavScore(goods.getFavScore());
			if(!gt.getProviderGoodsImgInfo().equals(goods.getProviderGoodsImgInfo())){
				gtTemp.setProviderGoodsImgInfo(goods.getProviderGoodsImgInfo());
			}
			if(!gt.getProviderGoodsName().equals(goods.getProviderGoodsName())){
				gtTemp.setProviderGoodsName(goods.getProviderGoodsName());
			}
			goods.setId(gt.getId());
			goodsInfoDao.updateByIdSelective(gtTemp);
		}
		return 1;
	}
	
	@Override
	public int resetGoodsScoreAndStatus(Integer providerId) {
		if(providerId == null)
			return 0;
		return goodsInfoDao.updateFavScoreAndStatusByProviderId(providerId ,null , GoodsStatusEnum.OFFLINE.ordinal(),null);
	}

	@Autowired
	private GoodsInfoDao goodsInfoDao;

	
	
}
